<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:03:41
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_favores extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_favores';
        $this->_pk            = 'favore_id';
        $this->_pre_cache_key = 'jamesonreadfavores_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    public function hasFavore($uid, $book_id)
    {
        return (int) DB::result_first('SELECT count(favore_id) FROM %t WHERE uid=%d AND book_id=%d', array($this->_table, $uid, $book_id));
    }
    public function insertAndBooks($data)
    {
        $res1 = $this->insert($data, true);
        $res2 = DB::query('UPDATE ' . DB::table('jamesonread_books') . ' SET ' . DB::field('favores', '1', '+') . ' WHERE ' . DB::field('book_id', $data['book_id'], '='));
        return $res1 && $res2;
    }
    public function count_by_uid($uid)
    {
        return (int) DB::result_first('SELECT count(favore_id) FROM %t WHERE uid=%d', array($this->_table, $uid));
    }
    public function fetch_by_page($uid, $start, $size)
    {
        $cacheid = 'fetch_by_page-'.$uid.'-'.$start.'-'.$size;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $limit = ' LIMIT ' . intval($start) . ',' . intval($size);
        if (!$size) {
            $limit = '';
        }
        $data = DB::fetch_all('SELECT f.book_id,f.uid,f.favore_id,b.book_name,b.image,b.hascol,b.author,b.addtime,c.subject,c.tid,c.pid,c.addtime as updatetime FROM %t AS f LEFT JOIN %t AS b ON f.book_id=b.book_id LEFT JOIN %t AS c ON f.book_id=c.book_id WHERE f.uid=%d AND b.book_id=f.book_id GROUP BY c.book_id  ORDER BY c.zhangjie DESC  ' . $limit, array($this->_table, 'jamesonread_books', 'jamesonread_colums', $uid));
        if ($data) {
            $this->store_cache($cacheid, $data);
        }
        return $data;
    }
}